package com.example.user.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class SpinnerActivity extends AppCompatActivity {

    private static final String[] countriesStr = { "墨染寺","博丽神社","蓬莱岛","红魔馆" };
    private TextView myTextView;
    private Spinner mySpinner;
    private ArrayAdapter adapter;
    Animation myAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner);


        myTextView = (TextView) findViewById(R.id.myTextView);
        mySpinner = (Spinner) findViewById(R.id.mySpinner);
        adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, countriesStr);
        /* dropdown为自定义下拉菜单样式定义在res/layout目录下 */
        adapter.setDropDownViewResource(R.layout.dropdown);
        /* 将ArrayAdapter加入Spinner对象中 */
        mySpinner.setAdapter(adapter);


        /* 将mySpinner加入OnItemSelectedListener */
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3)
            {
                /* 将所选mySpinner的值带入myTextView中 */
                myTextView.setText("选择的是" + countriesStr[arg2]);
                /* 将mySpinner显示 */
                arg0.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView arg0)
            {

            }
        });
        /* 取得Animation定义在res/anim目录下 */
        myAnimation = AnimationUtils.loadAnimation(this, R.anim.myanim);
        /* 将mySpinner加入OnTouchListener */
        mySpinner.setOnTouchListener(new Spinner.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                /* 将mySpinner执行Animation */
                v.startAnimation(myAnimation);
                /* 将mySpinner隐藏 */
                v.setVisibility(View.VISIBLE);
                return true;
            }
        });
        mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.spinner, menu);
        return true;
    }
}
